# 中药学歌诀生成
from docx.shared import RGBColor
from docx import Document

doc = Document(r'E:\BaiduNetdiskWorkspace\1701864\123同步文件夹\确有专长笔记\中药学\中药学笔记.docx')


def highlight_word(document, word_to_highlight):
    for paragraph in document.paragraphs:
        for run in paragraph.runs:
            if word_to_highlight in run.text:
                new_text = run.text.replace(word_to_highlight, '')
                print(new_text, word_to_highlight)
                run.bold = True
                run.font.color.rgb = RGBColor(255, 0, 0)
                run.text = new_text + word_to_highlight





# 打印所有段落的文本
list_ = []
num = 1  # 四级标题顺序
# 主药标题级别
masterStyle = "Heading 3"

# isDelXianDai = False  # 是否删除 【现代研究】
# isDelFuYao = False  # 是否删除附药
# isYaoWuJieShao = False  # 是否是药物介绍
# isDelKongGe = False  # 是否是空格
beforePara = None
beforeHeading = None
for para in doc.paragraphs:
    # print(para.style.name, para.text)
    # 若上一个是【歌诀】 这一个就加个【药物】
    # if beforeHeading and para.style.name == "Body Text":
    #     para._p.getparent().remove(para._p)
    #     continue
    if beforePara and beforePara.text.startswith("【歌诀】"):
        para.text = "【药物】" + para.text
    #
    # # 删除杂项
    if para.style.name not in ['Heading 1', 'Heading 2', 'Heading 3'] and para.text.strip().split('】')[0] not in ['【药物', '【歌诀']:
        para._p.getparent().remove(para._p)
        beforePara = para
        continue


    if para.style.name in ['Heading 3'] and not para.text.startswith('第'):
        para._p.getparent().remove(para._p)
        # continue
    print(para.style.name, para.text)
    if para.style.name.startswith("Heading"):
        beforeHeading = para
    beforePara = para





doc.save(r'C:\Users\Dapan1\Desktop\助理\中药学\中药学歌诀.docx')